package com.deng.hadoop.mapreduce.demo.weather;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created By DengLibin
 * Date: 19-4-3
 * Time: 上午9:37
 * Desc: 天气对象实体 hadoop中所有的key都要实现WritableComparable接口(可序列化，可比较)
 */
public class TianQi implements WritableComparable<TianQi> {

    private int year;//年

    private int month;//月

    private int day;//天

    private int temperature;//温度

    /**
     * 比较 按日期正序排列
     * @param o
     * @return
     */
    @Override
    public int compareTo(TianQi o) {
        int x = Integer.compare(this.year, o.year);
        if(x == 0){ //年份相等
            x = Integer.compare(this.month, o.month);
            if(x == 0){//月份也相等
                x = Integer.compare(this.day, o.day);
            }
        }
        return x;
    }

    /**
     * 序列化输出的字段
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(year);
        out.writeInt(month);
        out.writeInt(day);
        out.writeInt(temperature);
    }

    /**
     * 反序列化 读取字段
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt(); //读的字段和写的字段顺序一致
        this.month = in.readInt();
        this.day = in.readInt();
        this.temperature = in.readInt();
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }
}
